package guda.house.biz.impl;

import java.util.List;

import guda.house.biz.constants.CommonResultCode;
import guda.house.biz.enums.SysVarTypeEnum;
import guda.house.biz.query.OfficeEmployeeQuery;
import guda.house.biz.query.SysVarQuery;
import guda.house.dal.dao.SysVarDAO;
import guda.house.dal.dataobject.SysVarDO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import guda.house.biz.OfficeEmployeeBiz;
import guda.house.dal.dao.OfficeEmployeeDAO;
import guda.house.dal.dataobject.OfficeEmployeeDO;
import guda.grape.autogen.common.page.BaseQuery;
import guda.grape.autogen.common.page.BizResult;

public class OfficeEmployeeBizImpl implements OfficeEmployeeBiz{

    private final static Logger logger = LoggerFactory.getLogger(OfficeEmployeeBizImpl.class);

    @Autowired
    private OfficeEmployeeDAO officeEmployeeDAO;
    @Autowired
    private SysVarDAO sysVarDAO;

    public BizResult detail(long id) {
        BizResult bizResult = new BizResult();
        try{
            OfficeEmployeeDO officeEmployeeDO = officeEmployeeDAO.getOfficeEmployeeById(id);
            bizResult.data.put("officeEmployeeDO", officeEmployeeDO);
            bizResult.success = true;
        }catch(Exception e){
            logger.error("query OfficeEmployee error",e);
        }
        return bizResult;
}

    public BizResult list(OfficeEmployeeQuery officeEmployeeQuery) {
        BizResult bizResult = new BizResult();
        try {
            int totalCount = officeEmployeeDAO.countForPage(officeEmployeeQuery);
            officeEmployeeQuery.setTotalCount(totalCount);
            List<OfficeEmployeeDO> officeEmployeeList = officeEmployeeDAO.selectForPage(officeEmployeeQuery);
            bizResult.data.put("officeEmployeeList", officeEmployeeList);
            bizResult.data.put("query", officeEmployeeQuery);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("view OfficeEmployee list error", e);
        }
            return bizResult;
     }

    public BizResult delete(long id) {
        BizResult bizResult = new BizResult();
        try {
            officeEmployeeDAO.delOfficeEmployeeById(id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("delete officeEmployee error", e);
        }
        return bizResult;
    }

    public BizResult create(OfficeEmployeeDO officeEmployeeDO) {
        BizResult bizResult = new BizResult();
        try {
            SysVarDO sysVarById = sysVarDAO.getSysVarById(officeEmployeeDO.getVarId());
            if(sysVarById == null){
                bizResult.code = CommonResultCode.RECORD_NOT_FOUND;
                return bizResult;
            }
            officeEmployeeDO.setVarName(sysVarById.getVarValue());
            long id = officeEmployeeDAO.insert(officeEmployeeDO);
            bizResult.data.put("id", id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("create OfficeEmployee error", e);
        }
        return bizResult;
    }

    @Override
    public BizResult update(OfficeEmployeeDO officeEmployeeDO) {
        BizResult bizResult = new BizResult();
        try {
            SysVarDO sysVarById = sysVarDAO.getSysVarById(officeEmployeeDO.getVarId());
            if(sysVarById == null){
                bizResult.code = CommonResultCode.RECORD_NOT_FOUND;
                return bizResult;
            }
            officeEmployeeDO.setVarName(sysVarById.getVarValue());
            officeEmployeeDAO.updateByIdSelective(officeEmployeeDO);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("update OfficeEmployee error", e);
        }
        return bizResult;
    }

    @Override
    public BizResult queryForCreate(Long villageId) {
        BizResult bizResult = new BizResult();
        SysVarQuery sysVarQuery = new SysVarQuery();
        sysVarQuery.setVillageId(villageId);
        sysVarQuery.setVarType(SysVarTypeEnum.office.name());
        sysVarQuery.setPageSize(500);
        List<SysVarDO> sysVarDOs = sysVarDAO.selectForPage(sysVarQuery);
        bizResult.data.put("sysVarList",sysVarDOs);
        bizResult.success = true;
        return bizResult;
    }


}
